package com.zima.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zima.mall.entity.MallGoods;
import com.zima.mall.entity.MallOrder;
import com.zima.mall.entity.MallOrderItem;
import com.zima.mall.entity.MallUser;
import com.zima.mall.mapper.LoginInformationMapper;
import com.zima.mall.mapper.OrderItemMapper;
import com.zima.mall.mapper.OrdersMapper;
import com.zima.mall.mapper.ProductsMapper;
import com.zima.mall.service.OrderService;
import com.zima.mall.vo.ProductsAddQuantity;
import com.zima.mall.vo.TableVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    OrdersMapper ordersMapper;

    @Resource
    OrderItemMapper orderItemMapper;

    @Resource
    ProductsMapper productsMapper;

    @Resource
    LoginInformationMapper loginInformationMapper;

    @Override
    public TableVo findAllOrders(Integer page,Integer size) {
        QueryWrapper wrapper = new QueryWrapper();
        IPage<MallOrder> mallOrderIPage = new Page<>(page,size);
        IPage<MallOrder> result = ordersMapper.selectPage(mallOrderIPage,wrapper);
        TableVo tableVo = new TableVo();

        tableVo.setData(result.getRecords());
        tableVo.setTotalNum(ordersMapper.selectCount(wrapper));

        return tableVo;
    }

    @Override
    public TableVo findByOrderNo(Long orderNo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("goods_id","quantity");
        wrapper.eq("order_no",orderNo);

        List<MallOrderItem> goodsId = orderItemMapper.selectList(wrapper);//先通过订单编号找到该订单下所有商品id

        TableVo<MallGoods> goods = new TableVo<>();
        if (0 == goodsId.size()) return goods;//如果对应的订单编号在订单项目表中不存在的话则返回空的商品表单，否则继续

        QueryWrapper wrapper_goods = new QueryWrapper();
        for (MallOrderItem a:goodsId){//通过所有商品id去商品表查找对应商品信息
            wrapper_goods.eq("id",a.getGoodsId());
            wrapper_goods.or();
        }

        goods.setData(productsMapper.selectList(wrapper_goods));
        goods.setTotalNum(productsMapper.selectCount(wrapper_goods));

        TableVo goodsAddQuantityVo = new TableVo();
        List goodsAndQuantity = new LinkedList();
        for (MallGoods a:goods.getData()){
            ProductsAddQuantity tmp = new ProductsAddQuantity();

            tmp.setCreated(a.getCreated());
            tmp.setCategoryId(a.getCategoryId());
            tmp.setDetail(a.getDetail());
            tmp.setIconUrl(a.getIconUrl());
            tmp.setId(a.getId());
            tmp.setIsHot(a.getIsHot());
            tmp.setPrice(a.getPrice());
            tmp.setSpecParam(a.getSpecParam());
            tmp.setStatus(a.getStatus());
            tmp.setName(a.getName());
            tmp.setStock(a.getStock());
            tmp.setSubImages(a.getSubImages());
            tmp.setUpdated(a.getUpdated());

            for (MallOrderItem b:goodsId){
                if (b.getGoodsId() == a.getId()){
                    tmp.setQuantity(b.getQuantity());
                    continue;
                }
            }
            goodsAndQuantity.add(tmp);
        }
        goodsAddQuantityVo.setData(goodsAndQuantity);
        goodsAddQuantityVo.setTotalNum(goods.getTotalNum());

        return goodsAddQuantityVo;
    }

    @Override
    public int deleteOrder(Long id) {
        if (id == null) return 0;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_no",id);
        ordersMapper.delete(wrapper);
        orderItemMapper.delete(wrapper);
        return 1;//如果成功删除则返回值1
    }

    @Override
    public TableVo findByAccount(String account) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("account",account);
        MallUser mallUser = loginInformationMapper.selectOne(wrapper);

        QueryWrapper wrapper_orders = new QueryWrapper();
        wrapper_orders.eq("uid",mallUser.getUserId());

        TableVo<MallOrder> orderTableVo = new TableVo<>();
        orderTableVo.setData(ordersMapper.selectList(wrapper_orders));
        orderTableVo.setTotalNum(ordersMapper.selectCount(wrapper_orders));

        return orderTableVo;
    }


}
